<template>
  <div class="system-role-container layout-padding">

    <div class="system-role-padding layout-padding-auto layout-padding-view">
      <div class="system-user-search mb15">
        <el-input size="default" v-model="state.keyword" placeholder="请输入关键字" style="max-width: 180px" clearable> </el-input>
        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon> <ele-Search /> </el-icon>
          查  询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenDialog('add')">
          <el-icon><ele-CirclePlusFilled /></el-icon>
          新  增
        </el-button>
        <el-button size="default" type="danger" class="ml10" @click="onRowDel()">
          <el-icon><ele-DeleteFilled /></el-icon>
          删  除
        </el-button>
        <el-button size="default" type="primary" class="ml10" @click="refresh()">
          <el-icon> <ele-Refresh /> </el-icon>
          刷新任务
        </el-button>
      </div>

      <el-table :data="state.tableData.data" ref="multipleTable" v-loading="state.tableData.loading" stripe style="width: 100%">
        <el-table-column type="selection" width="50" />
        <el-table-column type="index" label="序号" width="60" />
<!--        <el-table-column prop="schedName" label="微服务代码" width="120px"  show-overflow-tooltip></el-table-column>-->
<!--        <el-table-column prop="schedCode" label="调度组代码" width="120px" show-overflow-tooltip></el-table-column>-->
        <el-table-column prop="jobGroupCode" label="任务组代码" width="150px"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="jobCode" label="任务代码" width="130px"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="jobName" label="任务名称" width="150px"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="cronExpression" label="cron表达式" width="120px"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="state" label="任务状态" width="100px"  show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" size="small" v-if="scope.row.state==='ACQUIRED'">运行</el-tag>
            <el-tag type="error" size="small" v-else>暂停</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="jobClass" label="执行类" width="300px"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="schedTime" label="下次执行时间" width="170px"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="jobData" label="任务数据" width="120px"  show-overflow-tooltip></el-table-column>
        <el-table-column prop="remark" label="备注" width="120px"  show-overflow-tooltip></el-table-column>
       
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenDialog('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
            <el-button size="small" text type="primary" @click="onLog(scope.row)">日志</el-button>
            <el-button size="small" text type="primary" @click="onRun(scope.row)">执行任务</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="(val: number)=>{state.tableData.param.pageSize = val; getTableData();}"
          @current-change="(val: number) => {state.tableData.param.pageNum = val;getTableData();}"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </div>

    <div class="system-dic-dialog-container">
      <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" draggable class="system-dialog">
        <el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
          <el-row :gutter="35">


             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="调度组代码">
                 <el-input v-model="state.ruleForm.schedCode" placeholder="请输入调度组代码" clearable></el-input>
               </el-form-item>
             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="任务组代码">
                 <el-input v-model="state.ruleForm.jobGroupCode" placeholder="请输入任务组代码" clearable></el-input>
               </el-form-item>
             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="任务代码">
                 <el-input v-model="state.ruleForm.jobCode" placeholder="请输入任务代码" clearable></el-input>
               </el-form-item>
             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="任务名称">
                 <el-input v-model="state.ruleForm.jobName" placeholder="请输入任务名称" clearable></el-input>
               </el-form-item>
             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="执行类">
                 <el-input v-model="state.ruleForm.jobClass" placeholder="请输入执行类" clearable></el-input>
               </el-form-item>
             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="cron表达式">
                 <el-input v-model="state.ruleForm.cronExpression" placeholder="请输入cron表达式" clearable></el-input>
               </el-form-item>
             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="任务状态">
                 <el-select v-model="state.ruleForm.state" placeholder="请输入任务状态">
                   <el-option
                       v-for="item in state.stateOptions"
                       :key="item.key"
                       :label="item.value"
                       :value="item.key"
                   />
                 </el-select>

               </el-form-item>

             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="任务数据">
                 <el-input v-model="state.ruleForm.jobData" placeholder="请输入任务数据" clearable></el-input>
               </el-form-item>
             </el-col>

             <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
               <el-form-item label="备注">
                 <el-input v-model="state.ruleForm.remark" placeholder="请输入备注" clearable></el-input>
               </el-form-item>
             </el-col>


          </el-row>
        </el-form>
        <template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialog.isShowDialog=false" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
        </template>
      </el-dialog>


      <el-dialog :title="state.dialog_log.title" v-model="state.dialog_log.isShowDialog" draggable class="system-dialog"
      style="height: 590px">
        <el-table :data="state.dialog_log.data" ref="multipleTable" v-loading="state.tableData.loading" stripe
                  style="width: 100%;height: 450px">
          <el-table-column type="selection" width="50" />
          <el-table-column type="index" label="序号" width="60" />
          <el-table-column prop="runTime" label="运行时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="endTime" label="结束时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="state" label="任务状态" width="100px"  show-overflow-tooltip>
            <template #default="scope">
              <el-tag type="success" size="small" v-if="scope.row.state==='1'">成功</el-tag>
              <el-tag type="success" size="small" v-else-if="scope.row.state==='-1'">运行中</el-tag>
              <el-tag type="error" size="small" v-else>异常</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>

        </el-table>

        <el-pagination
            @size-change="(val: number)=>{state.dialog_log.param.pageSize = val; getLog();}"
            @current-change="(val: number) => {state.dialog_log.param.pageNum = val;getLog();}"
            class="mt15"
            :pager-count="5"
            :page-sizes="[10, 20, 30]"
            v-model:current-page="state.dialog_log.param.pageNum"
            background
            v-model:page-size="state.dialog_log.param.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="state.dialog_log.total"
        >
        </el-pagination>
      </el-dialog>


    </div>

  </div>
</template>

<script setup lang="ts" name="systemDic">
import {reactive, onMounted, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import { api } from '/@/api/system/component_scheduler_api.ts';

// 定义变量内容
const multipleTable = ref();
interface RowData {
    id?: string;
    operateType?: number;
   schedCode?: string;
   jobGroupCode?: string;
   jobCode?: string;
   jobName?: string;
   jobClass?: string;
   cronExpression?: string;
   state?: string;
   jobData?: string;
   remark?: string;
 
}
// 数据绑定
const state = reactive({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
  keyword: '',
  ruleForm: <RowData>({}),
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  dialog_log:{
    schedulerId:'',
    isShowDialog: false,
    title:'运行日志',
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
  stateOptions:[{key:"ACQUIRED",value:"运行"}, {key:"PAUSED",value:"暂停"}]
});

//-------------------------------------------------------------------
// 打开弹窗
const onOpenDialog = (type: string, row ?: RowData) => {
  if (type === 'edit') {
    state.ruleForm = <RowData>(row);
    state.dialog.title = '修改任务调度';
    state.dialog.submitTxt = '修 改';
  } else {
    state.dialog.title = '新增任务调度';
    state.dialog.submitTxt = '新 增';
    state.ruleForm = <RowData>({ schedCode:'DefaultSchedCode', jobGroupCode:'DefaultJobGroup',state:'PAUSED'});
  }
  state.dialog.isShowDialog = true;
};
// 提交
const onSubmit = () => {
  state.dialog.isShowDialog = false;
  //保存
  var data = state.ruleForm;
  var isAdd = data.id ? false : true;
  data.operateType = (isAdd ? 1 : 2);
  state.tableData.loading = true;
  api().operate([data]).then(()=>{
    ElMessage.success(isAdd ?'新增成功':'修改成功');
    getTableData();
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {state.tableData.loading = false;})
};
// 删除
const onRowDel = (param ?: RowData) => {
  //获取选中的id
  let list: RowData[] = []
  if(param){
    list.push(param)
  }else{
    const selectionRows = multipleTable.value.getSelectionRows();
    if(!selectionRows || selectionRows.length == 0){
      ElMessage.error('请选择要操作的数据');
      return
    }
    list = selectionRows
  }
  // 设置值
  for (let i = 0; i < list.length; i++) {
    list[i].operateType=3; //删除
  }
  // 弹出框
  ElMessageBox.confirm(`是否永久删除 ?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    state.tableData.loading = true;
    api().operate(list).then(()=>{
        ElMessage.success('删除成功: ');
        getTableData();
    }).catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
      .finally(() => {state.tableData.loading = false;})
  }).catch(() => {});
};

// 删除
const onRun = (param ?: RowData) => {
  state.tableData.loading = true;
  api().runJob(param).then(()=>{
    ElMessage.success('执行成功: ');
  }).catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
      .finally(() => {state.tableData.loading = false;})
};

// 删除
const onLog = (param ?: RowData) => {
  state.dialog_log.isShowDialog = true;
  state.dialog_log.schedulerId = param?.id;
  state.dialog_log.title = param?.jobName + " - " + param?.jobCode;
  getLog()
};

const  getLog = () => {
  let logParam = {
    schedulerId:state.dialog_log.schedulerId,
    pageNum:state.dialog_log.param.pageNum,
    pageSize:state.dialog_log.param.pageSize
  }

  state.tableData.loading = true;
  api().list_log(logParam).then((res)=>{
    state.dialog_log.data = res.data.list;
    state.dialog_log.total = res.data.total;
  }).catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
      .finally(() => {state.tableData.loading = false;})
}

// 查询表格数据
const refresh = () => {
  const param = { }
  api().refresh(param).then(res=>{
    getTableData()
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => { })
};



//-------------------------------------------------------------------
// 查询表格数据
const getTableData = () => {
  state.tableData.loading = true;
  const param = {
    keyword: state.keyword,
    pageNum: state.tableData.param.pageNum,
    pageSize: state.tableData.param.pageSize
  }
  api().list(param).then(res=>{
    state.tableData.data = res.data.list;
    state.tableData.total = res.data.total;
  })
  .catch(err => {ElMessageBox.alert(err, '操作异常', {confirmButtonText: '确定'});})
  .finally(() => {state.tableData.loading = false;})
};

//-------------------------------------------------------------------
// 页面加载时
onMounted(() => {
  getTableData();
});

</script>


